﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NUS.ISS.ARS.BusinessFacade.Interface;
using NUS.ISS.ARS.BusinessFacade;
using NUS.ISS.ARS.BusinessEntity;
using NUS.ISS.ARS.BusinessEntity.ViewModels;


namespace NUS.ISS.ARS.Web.Admin.Controllers
{

    public class CancelBookingController : Controller
    {

        [HttpGet]
        public ActionResult CancelBooking()
        {
            IAdminBF a = BFFactory.CreateAdminBFInstance();
            IEnumerable<CancelBookingTripView> Cancelbookingtripviews;
            Cancelbookingtripviews = a.IEnumGetFlightBookings();
            return View(Cancelbookingtripviews);

        }

        [HttpPost]
        public ActionResult CancelBooking(String reference, String firstName, string lastName)
        {

            IAdminBF a = BFFactory.CreateAdminBFInstance();
            IEnumerable<CancelBookingTripView> Cancelbookingtripviews;
            if (!((string.IsNullOrEmpty(reference)) && (string.IsNullOrEmpty(firstName)) && (string.IsNullOrEmpty(lastName))))
            {

                Cancelbookingtripviews = a.GetFlightBookingsByRefNoAndFirstNameAndLastName(reference, firstName, lastName);
            }
            else
            {
                Cancelbookingtripviews = a.IEnumGetFlightBookings();

            }
            if (Request.IsAjaxRequest())
            {
                return PartialView(Cancelbookingtripviews);
            }
            else
            {
                return View(Cancelbookingtripviews);
            }
        }

        [HttpGet]
        public ActionResult CancelBookingDelete(long id)
        {
            IBookingBF a = BFFactory.CreateBookingBFInstance();
            List<long> flightbookingtripids=a.FlightBookingTripIdByFlightBookingId(id);
            CancelBookingTripView Cancelbookingtripview = new CancelBookingTripView();
            foreach (long flightbookingtripid in flightbookingtripids)
            {
                Cancelbookingtripview.flightbookingviews.Add(a.GetFlightBookingsById(flightbookingtripid)); 
            }
            Cancelbookingtripview.FlightBookingId = id; 
            return View(Cancelbookingtripview);
        }

        [HttpPost]
        public ActionResult CancelBookingDelete(CancelBookingTripView cancelbookingtripview)
        {

            IBookingBF a = BFFactory.CreateBookingBFInstance();
            CancelBookingTripView cancelBookingTripView = a.GetFlightBookingsByFlightBooking(cancelbookingtripview.FlightBookingId);
            cancelBookingTripView.IsCancelled = true;
            a.SaveChangedBooking(cancelBookingTripView);
            return RedirectToAction("CancelBooking");
        }

    }
}
